#include "bashapp.h"
#include <vector>

BashApp::BashApp()
{
    exit_ = false;
    ShowInputLine();
}

void BashApp::ParseCommand(string command_str) {
    vector<string> command;
    
    // 按照空格分割指令
    string buffer = "";
    for (size_t i = 0; i < command_str.length(); i ++) {
        if (command_str[i] != ' ') {
            buffer += command_str[i];
        }
        else {
            command.push_back(buffer);
            buffer = "";
        }
    }
    command.push_back(buffer);
    
    // 根据指令名执行函数
    string command_name = command[0];
    if (command_name == "cd") {
        Cd(command);
        return;
    }
    else if (command_name == "exit") {
        exit_ = true;
    }
    else if (command_name == "mkdir") {
        Mkdir(command);
        return;
    }
    else if (command_name == "ls") {
        Ls(command);
        return;
    }
    else if (command_name == "touch") {
        Touch(command);
        return;
    }
    else if (command_name == "watch") {
        Watch(command);
        return;
    }
    else if (command_name == "update") {
        Update(command);
        return;
    }
    else if (command_name == "delete") {
        Delete(command);
        return;
    }
    else if (command_name == "help") {
        cout << "cd directory_path        进入指定目录\n"
                "touch name.type          在当前目录下创建文件\n"
                "ls                       列出当前目录下的所有文件和目录\n"
                "mkdir name               在当前目录下创建目录\n"
                "update name.type content 更新当前目录下的文件内容\n"
                "watch name.type          查看当前目录下的文件内容\n"
                "delete name.type         删除当前目录下的文件\n"
                "exit                     退出当前会话"
             << endl;
    }
    else {
        ShowResult("未定义的命令");
    }
    
    return;
}

void BashApp::Cd(vector<string> command) {
    string path_name = command[1];
    
    if (path_name[0] != '/') {
        string path = file_system_->GetOpeningPath();
        if (path[path.length()-1] != '/') {
            path += '/';
        }
        path_name = path + path_name;
    }
    
    if (!file_system_->OpenDir(path_name)) {
        ShowResult("文件夹"+path_name+"不存在");
        return;
    }
}

void BashApp::Mkdir(vector<string> command) {
    File newFile(command[1], file_system_->GetOpeningPath());
    newFile.attribute_ = 0xC;
    file_system_->NewDir(newFile);
    
    ShowResult("创建成功");
}

void BashApp::Ls(vector<string> command) {
    string buffer = "";
    for (auto &f: *file_system_->GetOpeningPathDirItems()) {
        if (f.IsEmpty()) continue;
        if (f.IsDir()) {
            buffer += (f.name_ + " ");
        }
        else {
            buffer += (f.name_ + "."+ f.type_ + " ");
        }
    }
    ShowResult(buffer);
}

void BashApp::Touch(vector<string> command) {
    File newFile(command[1], file_system_->GetOpeningPath());
    file_system_->NewFile(newFile);
    
    ShowResult("创建成功");   
}

void BashApp::Watch(vector<string> command) {
    File file(command[1], file_system_->GetOpeningPath());
    file_system_->OpenFile(file.GetPathName());
    ShowResult(file_system_->GetOpeningFile()->content_);
}

void BashApp::Update(vector<string> command) {
    File file(command[1], file_system_->GetOpeningPath());
    file_system_->OpenFile(file.GetPathName());
    
    file_system_->GetOpeningFile()->content_ = command[2];
    file_system_->UpdateFile(*file_system_->GetOpeningFile());
    ShowResult("更新成功");
}

void BashApp::Delete(vector<string> command) {
    string opening_path = file_system_->GetOpeningPath();
    
    File file(command[1], file_system_->GetOpeningPath());
    file_system_->OpenFile(file.GetPathName());
    file_system_->DeleteFile(*file_system_->GetOpeningFile());
    
    file_system_->OpenDir(opening_path);
    ShowResult("删除成功");
}

